﻿#include "LoginPageScene.h"
#pragma execution_character_set("utf-8")

USING_NS_CC;
using namespace ui;

Scene * LoginPageScene::createScene()
{
	return LoginPageScene::create();
}

bool LoginPageScene::init()
{
	if (!Scene::init())
	{
		return false;
	}

	//背景
	auto *background = Sprite::create("res/Background.png");
	background->setPosition(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2);
	background->setScale(VISIBLE_SIZE.height / background->getContentSize().height);
	addChild(background);

	//人物
	auto *humam = Sprite::create("res/denglu2.png");
	humam->setPosition(VISIBLE_SIZE.width / 4, VISIBLE_SIZE.height / 2);
	humam->setScale(VISIBLE_SIZE.height / humam->getContentSize().height);
	addChild(humam);

	//底部红条
	auto *bottom = Sprite::create("res/denglu1.png");
	bottom->setPosition(VISIBLE_SIZE.width / 2, bottom->boundingBox().size.height / 2);
	bottom->setScale(VISIBLE_SIZE.width / humam->getContentSize().width);
	addChild(bottom);

	//注册登入背景
	auto *background_right = Sprite::create("res/denglu3.png");
	background_right->setPosition(VISIBLE_SIZE.width*0.72, VISIBLE_SIZE.height / 2.5);
	background_right->setScale(1.2f);
	addChild(background_right);

	//返回按钮
	Button *backBtn = Button::create("res/denglu8.png");
	backBtn->setPosition(Vec2(background_right->getPositionX()
		+ background_right->boundingBox().size.width/2
		-backBtn->boundingBox().size.width/2-10
		, background_right->getPositionY()
		+ background_right->boundingBox().size.height / 2
		- backBtn->boundingBox().size.height / 2 - 10));
	addChild(backBtn);

	//注册/登入文本
	auto *title = Label::createWithTTF("注册/登入", "FZCY_GBK.ttf", 42);
	title->setPosition(background_right->getPositionX(), backBtn->getPositionY());
	addChild(title);

	//♣
	auto *plum_blossom = Sprite::create("res/denglu4.png");
	plum_blossom->setPosition(
		background_right->getPositionX() 
		- background_right->getBoundingBox().size.width / 2
		+ plum_blossom->getBoundingBox().size.width + 30
		, backBtn->getPositionY() - 130);
	plum_blossom->setScale(1.2f);
	addChild(plum_blossom);

	//账号文本
	auto userNameLabel = Label::create("账户:", "FZCY_GBK.ttf", 35);
	userNameLabel->setPosition(Vec2(
		plum_blossom->getPositionX()
		+ plum_blossom->getBoundingBox().size.width / 2
		+ userNameLabel->getBoundingBox().size.width / 2 + 30,
		plum_blossom->getPositionY()
		));
	addChild(userNameLabel);

	//账户输入框
	userNameText = ui::EditBox::create(cocos2d::Size(330, 50), ui::Scale9Sprite::create("res/denglu11.png"));
	userNameText->setPosition(Vec2(userNameLabel->getPositionX() + userNameLabel->getBoundingBox().size.width / 2 + userNameText->getBoundingBox().size.width / 2 + 30, userNameLabel->getPositionY()));
	userNameText->setPlaceHolder("请输入你的账号");
	//设置输入的模型
	//userNameText->setInputMode(ui::EditBox::InputMode::ANY);
	userNameText->setPlaceholderFont("宋体", 25);
	addChild(userNameText);

	//♠
	auto *spade = Sprite::create("res/denglu5.png");
	spade->setPosition(
		plum_blossom->getPositionX(),
		plum_blossom->getPositionY()
		-spade->getBoundingBox().size.width/2-50);
	spade->setScale(1.2f);
	addChild(spade);

	//密码文本
	auto passwordLabel = Label::create("密码:", "FZCY_GBK.ttf", 35);
	passwordLabel->setPosition(Vec2(
		spade->getPositionX()
		+ spade->getBoundingBox().size.width / 2
		+ passwordLabel->getBoundingBox().size.width / 2 + 30,
		spade->getPositionY()
	));
	addChild(passwordLabel);

	//密码输入框
	passwordText = ui::EditBox::create(cocos2d::Size(330, 50), ui::Scale9Sprite::create("res/denglu11.png"));
	passwordText->setPosition(Vec2(userNameText->getPositionX(), spade->getPositionY()));
	passwordText->setInputFlag(ui::EditBox::InputFlag::PASSWORD);
	passwordText->setPlaceHolder("请输入你的密码");
	passwordText->setPlaceholderFont("宋体", 25);
	passwordText->setInputFlag(ui::EditBox::InputFlag::PASSWORD);//密文
	addChild(passwordText);

	//记住账号
	auto remenberRadio = CheckBox::create("res/denglu10.png", "res/denglu9.png");
	remenberRadio->setPosition(Vec2(passwordLabel->getPositionX(), passwordLabel->getPositionY() - remenberRadio->getBoundingBox().size.height - 40));
	addChild(remenberRadio);

	//记住账号文本
	auto remenberLabel = Label::create("记住账号", "FZCY_GBK.ttf", 28);
	remenberLabel->setPosition(Vec2(remenberRadio->getPositionX() + remenberLabel->getBoundingBox().size.width / 2 + 30, remenberRadio->getPositionY()));
	addChild(remenberLabel);

	//自动登录
	auto autoLoginRadio = CheckBox::create("res/denglu10.png", "res/denglu9.png");
	autoLoginRadio->setPosition(Vec2(background_right->getPositionX() + 50, remenberLabel->getPositionY()));
	addChild(autoLoginRadio);

	//自动登录文本
	auto autoLoginLabel = Label::create("记住账号", "FZCY_GBK.ttf", 28);
	autoLoginLabel->setPosition(Vec2(autoLoginRadio->getPositionX() + autoLoginLabel->getBoundingBox().size.width / 2 + 30, autoLoginRadio->getPositionY()));
	addChild(autoLoginLabel);

	//注册按钮
	auto regBtn = Button::create("res/denglu6.png");
	regBtn->setScale(0.9);
	regBtn->setPosition(Vec2(background_right->getPositionX() - regBtn->getBoundingBox().size.width / 2 - 50, autoLoginLabel->getPositionY() - regBtn->getBoundingBox().size.height / 2 - 50));
	//regBtn->addTouchEventListener(CC_CALLBACK_2())
	addChild(regBtn);

	//登录按钮
	auto loginBtn = Button::create("res/denglu7.png");
	loginBtn->setScale(0.9);
	loginBtn->setPosition(Vec2(background_right->getPositionX() + loginBtn->getBoundingBox().size.width / 2 + 50, regBtn->getPositionY()));
	loginBtn->addTouchEventListener(CC_CALLBACK_2(LoginPageScene::login, this));
	addChild(loginBtn);
	
	//logo
	auto logo = Sprite::create("res/logo.png");
	logo->setScale(0.6);
	logo->setPosition(background_right->getPositionX(), background_right->getPositionY() + background_right->getBoundingBox().size.height / 2 + logo->getBoundingBox().size.height / 2 + 10);
	addChild(logo);

	return true;
}

void LoginPageScene::login(Ref * pSender, Widget::TouchEventType type)
{
	auto userName = userNameText->getText();
	auto password = passwordText->getText();
	if (type == Widget::TouchEventType::ENDED) {
		log(userName);
		log(password);
		TransitionFade* transitionFade = TransitionFade::create(3, Scene0910::createScene());
		Director::getInstance()->replaceScene(transitionFade);
	}
}
